﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Threading;
using System.Globalization;
using System.Data;

public partial class EditCompany : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
     
        if (!IsPostBack)
        {
            if (Request.QueryString["cusid"] != null)
            {
               
                string cus = Request.QueryString["cusid"];

                ViewState["client"] = cus;
                ////////////////////
                country.DataBind();
                city.DataBind();
                //////////////////////
                change_cus(cus);
            }
        }
    }
    protected void change_cus(string user)
    {

        DataTable ds = ((DataView)mainsource.Select(DataSourceSelectArguments.Empty)).Table;//DB.loadCustomerInfo2(Int64.Parse(user));
        //////////////adjust main info//////////////////////////
        DataRow dr = ds.Rows[0];

        // DataRow dr = DB.loadBasicCustomerInfo(Int64.Parse(user));
        //DataRow dr = dt.Rows[0];
        surname.Text = dr["companydesc"].ToString();
        companyname.Text = dr["company"].ToString();
        address.Text = dr["address"].ToString();
        email1.Text = dr["email1"].ToString();
     
        postalcode.Text = dr["postalcode"].ToString();
        District.Text = dr["district"].ToString();
      //  tele2.Text = dr["tele2"].ToString();
      //  fax.Text = dr["fax"].ToString();
        username.Text = dr["username"].ToString();
      //  pass.Text = dr["password"].ToString();
        ListItem ls = country.Items.FindByValue(dr["country_id"].ToString());
        country.SelectedIndex = country.Items.IndexOf(ls);
        ListItem lms = orgtype.Items.FindByValue(dr["organization_id"].ToString());
        orgtype.SelectedIndex = orgtype.Items.IndexOf(lms);
        ListItem cls = city.Items.FindByValue(dr["city_id"].ToString());
        city.SelectedIndex = city.Items.IndexOf(cls);
     string code  = country.SelectedItem.Text.Split(',')[1];

     tele1.Text = dr["tele1"].ToString();
     tele2.Text = dr["tele2"].ToString();
        code1.Text = code;
        code2.Text = code;
        code3.Text = code;

        GoogleMapForASPNet1.GoogleMapObject.Points.Clear();
        if (dr["longitude"].ToString() != "")
        {


            double longs = double.Parse(dr["longitude"].ToString());
            double lats = double.Parse(dr["latitude"].ToString());
            GooglePoint GP1 = new GooglePoint();

            GP1.ID = dr["address"].ToString();
            GP1.Latitude = lats;
            GP1.Longitude = longs;

            GP1.InfoHTML = dr["address"].ToString();

            GP1.IconImage = "icons/pushpin-yellow.png";

            GoogleMapForASPNet1.GoogleMapObject.CenterPoint = new GooglePoint("previous", lats + 0.05, longs - 0.05);
            GoogleMapForASPNet1.GoogleMapObject.Points.Add(GP1);

        }



      
    }
    protected override void InitializeCulture()
    {
        if (Session["CurrentCulture"] != null)
        {
            string lang = Session["CurrentCulture"].ToString();
            if (lang != null && lang != "")
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
                

            }
        }

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "reports", "there();", true);
        // div1.Visible = true;
        GoogleMapForASPNet1.setseen();
    }

    protected void country_SelectedIndexChanged(object sender, EventArgs e)
    {
       
        string code = country.SelectedItem.Text.Split(',')[1];
        code = code.Trim();
        code1.Text = code;
        code2.Text = code;
        code3.Text = code;
     /////////
    //    SET NOCOUNT ON insert into products( issn,product_type_id,product,publisher,productdesc,topic)values
 //('a3887891287',1,'a1648, war and peace in Europe  %cedited by Klaus Bussmann and Heinz Schilling.','a[M{uml}unster? :%bs.n.,%c1999?]','a M{uml}unster Osnabr{uml}uck 24010.1998-17.1.1999. ','aThirty Years War, 1618-1648%vExhibitions.'); select scope_identity()


    }
    protected void save_Click(object sender, EventArgs e)
    {

        if (Page.IsValid)
        {
            List<string> fields = new List<string>();
            ArrayList values = new ArrayList();
            List<string> types = new List<string>();
            ///////////////


            fields.Add("companydesc");
            types.Add("string");
            values.Add(surname.Text);
            /////////////////////////////////////////////////
            fields.Add("company");
            types.Add("string");
            values.Add(companyname.Text);
            ///////////////////////
            fields.Add("organization_id");
            types.Add("int");
            values.Add(orgtype.SelectedValue);
            /////////////////////////////////////////////////
            ///////////////////////
            fields.Add("organization");
            types.Add("string");
            values.Add(orgtype.SelectedItem.Text);
            /////////////////////////////////////////////////
            fields.Add("country_id");
            types.Add("string");
            values.Add(country.SelectedValue);
            ///////////////////////
            fields.Add("country");
            types.Add("string");
            values.Add(country.SelectedItem.Text);
            ///////////////////////
            fields.Add("city_id");
            types.Add("string");
            values.Add(city.SelectedValue);
            ///////////////////////
            fields.Add("city");
            types.Add("string");
            values.Add(city.SelectedItem.Text);
            ///////////////////////
            fields.Add("address");
            types.Add("string");
            values.Add(address.Text);
            ///////////////////////
            ///////////////////////
            fields.Add("district");
            types.Add("string");
            values.Add(District.Text);
            ///////////////////////
            ///////////////////////
            fields.Add("postalcode");
            types.Add("string");
            values.Add(postalcode.Text);
            ///////////////////////
            fields.Add("tele1");
            types.Add("string");
            values.Add(code1.Text+tele1.Text);
            ///////////////////////
            fields.Add("tele2");
            types.Add("string");
            values.Add(code1.Text+tele2.Text);
            ///////////////////////
            fields.Add("fax");
            types.Add("string");
            values.Add(code1.Text + fax.Text);
            ///////////////////////
            fields.Add("email1");
            types.Add("string");
            values.Add(email1.Text);
            ///////////////////////
            /////////////////////////////////////////////////
          
            fields.Add("regdate");
            types.Add("datetime");
            values.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", new CultureInfo("en-US")));
            /////////////////////////////////////////////////
            ///////////////////////
            int pointcount = GoogleMapForASPNet1.GoogleMapObject.Points.Count;
            if (pointcount > 0)
            {
                string bk = GoogleMapForASPNet1.GoogleMapObject.Points[0].IconImage;
                double longs;
                double lats;

                longs = GoogleMapForASPNet1.GoogleMapObject.Points[pointcount-1].Longitude;
                lats = GoogleMapForASPNet1.GoogleMapObject.Points[pointcount - 1].Latitude;
               
                ///////////////////////
                fields.Add("longitude");
                types.Add("decimal");
                values.Add(longs);
                ///////////////////////
                fields.Add("latitude");
                types.Add("decimal");
                values.Add(lats);
                ///////////////////////
            }
            ///////////////////////
            string myid = "";

            myid = Request.QueryString["cusid"];

            int f = DB.UPDATE("companies", "int,id," + myid, fields, types, values);
            if (f > 0)
            {
                fields.Clear();
                types.Clear();
                values.Clear();
                ////////////////////////
                ///////////////////////
                fields.Add("username");
                types.Add("string");
                values.Add(username.Text);
                ///////////////////////
                fields.Add("password");
                types.Add("string");
                values.Add(pass.Text);
                ///////////////////////
                fields.Add("role_id");
                types.Add("int");
                values.Add(2);
                ///////////////////////
                DB.UPDATE("users","int,company_id," + myid, fields, types, values);
                Response.Redirect("~/ManagerDefault.aspx");
            }
            ///////////////////////
        }
    }
    protected void cancels(object sender, EventArgs e)
    {
        Response.Redirect("~/MainForm2.aspx");

    }
   
    
    protected void enter_Click(object sender, EventArgs e)
    {

        int x=GoogleMapForASPNet1.GoogleMapObject.Points.Count ;
        if (x > 0)
        {
            address.Text = GoogleMapForASPNet1.GoogleMapObject.Points[x - 1].InfoHTML;
        }
        

    }


    
}
